package ru.vsu.cs.kis.elections.server;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

import ru.vsu.cs.kis.elections.socketexchenge.commands.CommandName;

public class ServerTask implements Runnable {

	private Socket socket = null;
	private OutputStream output = null;
	private InputStream input = null;

	public ServerTask(Socket aSocket) {
		try {
			this.socket = aSocket;
			this.output = socket.getOutputStream();
			this.input = socket.getInputStream();
		} catch (IOException e) {
			System.err.println(e.getStackTrace());
		}
	}

	@Override
	public void run() {
		Integer result = -1;
		try {
			CommandName command = new CommandName(output,input);
			if (command.executeOnServer() > 0) {
				result = command.getCommand().executeOnServer();
			}
			if (result >= 0) {
				System.out.println("ServerTask success");
			} else {
				System.out.println("ServerTask failed");
			}
			output.close();
			input.close();
			socket.close();
		} catch (IOException e) {
			System.err.println("server command failed");
			System.err.println(e.getStackTrace());
		}
	}
}
